package com.zaoshui.hudada.scoring;

import com.zaoshui.hudada.common.ErrorCode;
import com.zaoshui.hudada.exception.BusinessException;
import com.zaoshui.hudada.model.entity.App;
import com.zaoshui.hudada.model.entity.UserAnswer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/*
* 评分策略执行器
* */
@Service
public class ScoringStrategyExecutor{

    @Resource
    private List<ScoringStrategy> scoringStrategyList;

    public UserAnswer doScore(List<String> choices, App app) {
        // 1.判断参数是否为空
        Integer appType = app.getAppType();
        Integer scoringStrategy1 = app.getScoringStrategy();
        if(app == null || scoringStrategy1 == null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"应用配置错误,未能找到匹配的策略");
        }
        // 2.遍历ScoringStrategy实现类的集合，根据app的类型和评分策略创建出对应的评分策略对象执行方法
        for (ScoringStrategy scoringStrategy : scoringStrategyList) {
            if(scoringStrategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class)){
                //根据反射获取到评分策略对象上注解的应用类型和评分策略
                ScoringStrategyConfig scoringStrategyConfig = scoringStrategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                if(scoringStrategyConfig.appType() == appType && scoringStrategyConfig.scoringStrategy() == scoringStrategy1){
                    //执行评分策略
                    return scoringStrategy.doScore(choices,app);
                }
            }
        }
        throw new BusinessException(ErrorCode.SYSTEM_ERROR,"应用配置错误,未能找到匹配的策略");
    }
}
